 
package com.zdkj.subway.rca.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.excel.util.DateUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zdkj.common.core.exceltools.ExcelUtil;
import com.zdkj.common.core.exceltools.MultipartFileToFile;
import com.zdkj.common.security.util.ObjectUtils;
import com.zdkj.subway.rca.entity.SwCkStatementConfirmBankQrConsume;
import com.zdkj.subway.rca.entity.SwCkStatementConfirmBankQrConsumeTmp;
import com.zdkj.subway.rca.mapper.SwCkStatementConfirmBankQrConsumeTmpMapper;
import com.zdkj.subway.rca.service.SwCkStatementConfirmBankQrConsumeService;
import com.zdkj.subway.rca.service.SwCkStatementConfirmBankQrConsumeTmpService;
import com.zdkj.subway.rca.util.excel.RcaCommonExcelParse;
import com.zdkj.subway.rca.util.excel.RcaIExcelParser;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.math.BigDecimal;
import java.text.ParseException;
import java.time.ZoneId;
import java.util.*;

/**
 * 西安地铁建行TVM确认临时表
 *
 * @author stw
 * @date 2020-10-08 20:28:13
 */
@Service
public class SwCkStatementConfirmBankQrConsumeTmpServiceImpl extends ServiceImpl<SwCkStatementConfirmBankQrConsumeTmpMapper, SwCkStatementConfirmBankQrConsumeTmp> implements SwCkStatementConfirmBankQrConsumeTmpService {
    private String parsePattern = "yyyy-MM-dd HH:mm:ss";
    @Autowired
    private SwCkStatementConfirmBankQrConsumeService swCkStatementConfirmBankQrConsumeService;

    @Override
    public Boolean saveBatchWithExcel(final File excelFile) throws Exception {
        final Workbook workbook = ExcelUtil.chooseWorkbook(excelFile);
        final Sheet sheet = workbook.getSheetAt(0);
        final RcaIExcelParser excelParser = new RcaCommonExcelParse();
        //此处固定excel标题头第几行开始，第几列结束
        int rowNum = 2;
        int count = 4;
        final List<SwCkStatementConfirmBankQrConsumeTmp> listParser = excelParser.parse(sheet, new SwCkStatementConfirmBankQrConsumeTmp(),rowNum,count);
        if (CollectionUtil.isNotEmpty(listParser)) {
            List<SwCkStatementConfirmBankQrConsumeTmp> listDb = new ArrayList<>();
            MultipartFileToFile.delteTempFile(excelFile);
            for (SwCkStatementConfirmBankQrConsumeTmp tmpObj : listParser) {
                if (StringUtils.isEmpty(tmpObj.getTradeDateTmp())
                        || StringUtils.isEmpty(tmpObj.getCountTmp())
                        || StringUtils.isEmpty(tmpObj.getMoneyTmp())
                        || StringUtils.isEmpty(tmpObj.getServiceChargeTmp())) {
                    continue;
                }
                if (!parseDate(tmpObj.getTradeDateTmp())) {
                    continue;
                }
                final SwCkStatementConfirmBankQrConsumeTmp obj = ObjectUtils.getDefaultObj(SwCkStatementConfirmBankQrConsumeTmp.class);
                BeanUtils.copyProperties(tmpObj, obj, "deptId", "creatorId", "creatorName");
                checkErr(obj);
                listDb.add(obj);
            }
            Map<String, Object> columnMap = new HashMap<>();
            columnMap.put("CREATOR_ID", listDb.get(0).getCreatorId());
            this.baseMapper.deleteByMap(columnMap);
            return this.saveBatch(listDb);
        }

        return false;
    }

    private void checkErr(SwCkStatementConfirmBankQrConsumeTmp obj) {
        //更新字段
        String tradeDateTmp = obj.getTradeDateTmp();
        try {
            Date tradeDate = DateUtils.parseDate(tradeDateTmp, parsePattern);
            obj.setTradeDate(tradeDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
        } catch (Exception e) {
            obj.setErrMsg("交易日期 为空或格式错误");
        }

        try {
            String countTmp = obj.getCountTmp();
            Long count = Long.parseLong(countTmp);
            obj.setCount(count);
        } catch (Exception e) {
            obj.setErrMsg("笔数 为空或格式错误");
        }

        try {
            String moneyTmp = obj.getMoneyTmp();
            BigDecimal money = new BigDecimal(moneyTmp);
            BigDecimal num = new BigDecimal("100");
            money = money.multiply(num);
            obj.setMoney(money.longValue());
        } catch (Exception e) {
            obj.setErrMsg("金额 为空或格式错误");
        }

        try {
            String serviceChargeTmp = obj.getServiceChargeTmp();
            BigDecimal serviceCharge = new BigDecimal(serviceChargeTmp);
            BigDecimal num = new BigDecimal("100");
            serviceCharge = serviceCharge.multiply(num).setScale(2, BigDecimal.ROUND_HALF_UP);
            obj.setServiceCharge(serviceCharge.longValue());
        } catch (Exception e) {
            obj.setErrMsg("手续费 为空或格式错误");
        }

        //判断数据是否存在
        SwCkStatementConfirmBankQrConsume swCkStatementConfirmBankQrConsume = new SwCkStatementConfirmBankQrConsume();
        BeanUtils.copyProperties(obj, swCkStatementConfirmBankQrConsume, "deptId","creatorId","creatorName","createTime");
        List<SwCkStatementConfirmBankQrConsume> list = swCkStatementConfirmBankQrConsumeService.list(Wrappers.query(swCkStatementConfirmBankQrConsume));
        if (list != null && !list.isEmpty()) {
            obj.setErrMsg("数据已存在");
        }
    }

    private boolean parseDate(String string) {
        boolean convertSuccess = true;
        if (string == null) {
            return false;
        }

        try {
            DateUtils.parseDate(string, parsePattern);
        } catch (ParseException e) {
            convertSuccess = false;
        }
        return convertSuccess;
    }

    @Override
    public void saveBatchWithExcelOk(Integer creatorId) {
        //导入先删除
        this.baseMapper.deleteBatchWithExcelOk(creatorId);
        //导入先删除再添加包括重复的
        this.baseMapper.saveBatchWithExcelOkRepeat(creatorId);
        //this.baseMapper.saveBatchWithExcelOk(creatorId);
        saveBatchWithExcelCancel(creatorId);
    }

    @Override
    public void saveBatchWithExcelCancel(Integer creatorId) {
        Map<String, Object> columnMap = new HashMap<>();
        columnMap.put("CREATOR_ID", creatorId);
        this.baseMapper.deleteByMap(columnMap);
    }
}
